<template>
  <view>
    <view class="u-page">
      <view class="filterObject">
        <u-cell-group>
          <view v-for="(item, index) in filterObject.items" :key="index">
            <u-cell
              :title="item.label"
              :value="item.value || item.placeholder"
              :isLink="true"
              @click="doTreePicker(item)"
            ></u-cell>
          </view>
        </u-cell-group>
      </view>
      <view class="dklb" v-if="dkList.length!=0">
        <view v-for="(item, index) in dkList" :key="index">
          <u-line-progress :percentage="0" height="8"></u-line-progress>
          <u-cell-group>
            <u-cell title="姓名" :value="item.userName"></u-cell>
            <u-cell title="身份证" :value="item.credentialsNo"></u-cell>
            <u-cell title="打卡场所" :value="item.addr"></u-cell>
          </u-cell-group>
        </view>
      </view>
      <view class="dklb" v-else style="padding:20px">
          暂无查验历史...
      </view>

      <view class="buttomBtn">
        <u-button text="扫一扫" type="primary" @click="doQrScan()"></u-button>
      </view>
    </view>
  </view>
</template>

<script>
import getLocation from "@/utils/getLocation.js";
import { mapState, mapMutations } from "vuex";
export default {
  mixins: [getLocation],
  data() {
    return {
      dkList: [],
      pageStyle: "",
      qrcode: "",
      name: "",
      address: "",
      filterObject: {
        items: [
          {
            type: "tree-picker",
            label: "查验商户",
            options: [],
            value: "",
            optionApi:
              "/mobile/h5/covid19/apiShm/getList.jsp?openId=【openId】&status=审核通过&pageSize=50",
            placeholder: "请选择商户",
            autoSearch: true,
            nullPlaceholder: "您旗下没有商户",
          },
        ],
        queryObj: {
          companyId: "",
        },
      },
    };
  },
  onShow() {
    let pages = getCurrentPages();
    let currPage = pages[pages.length - 1];
    if (currPage.data.callBackData) {
      this.treePickerConfirm(currPage.data.callBackData);
    }
    currPage.data.callBackData = undefined;
  },
  onLoad(options) {
    var vm = this;
    vm.doWxLogin().then((res) => {
      vm.openId = res.openId;
      vm.getUserLocation().then((res) => {
        vm.longitude = res.longitude;
        vm.latitude = res.latitude;
      });
    });
  },
  methods: {
    ...mapMutations(["setPosition"]),

    doTreePicker: function (item) {
      this.currentFormField = item;
      uni.setStorageSync("currentPicked", item.value || []);
      if (item.pickerPage) {
        uni.navigateTo({
          url: item.pickerPage,
        });
      } else {
        uni.navigateTo({
          url:
            "/pages/tree/index?autoSearch=" +
            item.autoSearch +
            "&title=" +
            item.placeholder +
            "&loadApi=" +
            encodeURIComponent(this.getFmtStr(item.optionApi)),
        });
      }
    },
    doQrScan: function () {
      var vm = this;
      if (!this.companyId) {
        uni.$u.toast("请先选择商户");
        return;
      }
      uni.scanCode({
        onlyFromCamera: true, //只能从相机中获取，不能从图片中获取
        success: function (res) {
          //获取扫码结果
          // uni.$u.toast(JSON.stringify(res.result))
          vm.content = JSON.stringify(res.result);

          // 跳转
          uni.navigateTo({
            url: "/pages/shanghuma/chayan?info=" + res.result+'&companyId='+vm.companyId + '&companyName=' + vm.companyName,
          });
        },
        fail: function (res) {
          uni.$u.toast("扫码失败");
        },
      });
    },
    getFmtStr(str, n) {
      var vm = this;
      return str
        .replace(
          /【([^\x00-\xff]|[a-zA-Z_$])([^\x00-\xff]|[a-zA-Z0-9_$])*】/g,
          function (d) {
            var prop = d.slice(1, -1);
            if (prop == "openId") {
              return vm.openId;
            }
            return n[prop];
          }
        )
        .replace(
          /《([^\x00-\xff]|[a-zA-Z_$])([^\x00-\xff]|[a-zA-Z0-9_$])*》/g,
          function (d) {
            var prop = d.slice(1, -1);
            return vm.filterObject.queryObj[prop];
          }
        );
    },
    treePickerConfirm: function (data) {
        var vm=this;
      var d = data[0];
      this.currentFormField.value = d.name;
      // debugger
      this.companyId = d.id;
      this.companyName = d.name;
 
      vm.getAndLoad(
        "查验历史",
        `/mobile/ymt/cxhzsm/smqx/getShmHistory.jsp?shmId=${this.companyId}&page=1&limit=100`,
        function (res) {
            vm.dkList=res.data
        }
      );
    },
  },
};
</script>

<style lang="scss" scoped>
.dklb {
  height: 80vh;
  overflow: auto;
}
.u-page {
  height: 100vh;
  overflow: auto;
  background-size: 100%;
  background-color: #f5f6f6;
}

.btn {
  margin: 30rpx 30rpx;
  width: 690rpx;
  height: 220rpx;
}

.buttomBtn {
  position: fixed;
  width: 100%;
  padding: 20rpx;
  box-sizing: border-box;
  bottom: 0;
}
</style>